package com.ruoyi.supermarket.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.domain.SupermarketSale;
import com.ruoyi.supermarket.domain.SupermarketShelf;
import com.ruoyi.supermarket.service.ISupermarketComShelfService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.SupermarketCommodity;
import com.ruoyi.supermarket.service.ISupermarketCommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 超市商品信息Controller
 *
 * @author faye
 * @date 2024-05-25
 */
@RestController
@RequestMapping("/supermarket/commodity")
public class SupermarketCommodityController extends BaseController
{
    @Autowired
    private ISupermarketCommodityService supermarketCommodityService;

    @Autowired
    private ISupermarketComShelfService supermarketComShelfService;

    /**
     * 查询超市商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupermarketCommodity supermarketCommodity)
    {
        startPage();
        List<SupermarketCommodity> list = supermarketCommodityService.selectSupermarketCommodityList(supermarketCommodity);
//        if(!list.isEmpty()){
//            for(SupermarketCommodity com:list){
//                List<SupermarketShelf> shelfList=supermarketComShelfService.selectShelfByComId(com.getCommodityId());
//                System.out.println(shelfList);
//                StringBuilder stringBuilder=new StringBuilder("");
//                shelfList.forEach(item->{
//                    stringBuilder.append(item.getShelfLocation());
//                });
//                com.setRemark(shelfList.toString());
//                System.out.println(stringBuilder);
//            }
//        }

        //System.out.println(list);
        return getDataTable(list);
    }


    @GetMapping("/echarts/stockBar")
    public AjaxResult stockBar()
    {
        SupermarketCommodity com=new SupermarketCommodity();

        List<SupermarketCommodity> list = supermarketCommodityService.selectSupermarketCommodityList(com);
       // System.out.println(list);

        Map<String,Object> Listmap=new HashMap<>();
        List<String> nameList=new ArrayList<>();
        List<Long> stockList=new ArrayList<>();


        for(SupermarketCommodity item:list){
            nameList.add(item.getCommodityName());
            stockList.add(item.getQuantity());
        }

        Listmap.put("nameList",nameList);
        Listmap.put("stockList",stockList);

        return success(Listmap);
    }


    /**
     * 导出超市商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:export')")
    @Log(title = "超市商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupermarketCommodity supermarketCommodity)
    {
        List<SupermarketCommodity> list = supermarketCommodityService.selectSupermarketCommodityList(supermarketCommodity);
        ExcelUtil<SupermarketCommodity> util = new ExcelUtil<SupermarketCommodity>(SupermarketCommodity.class);
        util.exportExcel(response, list, "超市商品信息数据");
    }

    /**
     * 获取超市商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:query')")
    @GetMapping(value = "/{commodityId}")
    public AjaxResult getInfo(@PathVariable("commodityId") Long commodityId)
    {
        return success(supermarketCommodityService.selectSupermarketCommodityByCommodityId(commodityId));
    }

    /**
     * 新增超市商品信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:add')")
    @Log(title = "超市商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupermarketCommodity supermarketCommodity)
    {
        return toAjax(supermarketCommodityService.insertSupermarketCommodity(supermarketCommodity));
    }

    /**
     * 修改超市商品信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:edit')")
    @Log(title = "超市商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupermarketCommodity supermarketCommodity)
    {
        return toAjax(supermarketCommodityService.updateSupermarketCommodity(supermarketCommodity));
    }

    /**
     * 删除超市商品信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:commodity:remove')")
    @Log(title = "超市商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commodityIds}")
    public AjaxResult remove(@PathVariable Long[] commodityIds)
    {
        return toAjax(supermarketCommodityService.deleteSupermarketCommodityByCommodityIds(commodityIds));
    }
}
